package com.steve.xfood.controller;

import com.steve.xfood.dataobject.ProductCategory;
import com.steve.xfood.enums.ResultEnum;
import com.steve.xfood.exception.XFoodException;
import com.steve.xfood.form.CategoryForm;
import com.steve.xfood.service.ProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 卖家类目
 * @author steve
 * @date 2020/5/25 10:54
 */
@Controller
@RequestMapping("/seller/category")
public class SellerCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 商品类目列表
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(Map<String, Object> map) {
        List<ProductCategory> categoryList = productCategoryService.findAll();
        map.put("categoryList",categoryList);
        return new ModelAndView("category/list", map);
    }

    /**
     * 商品类目数据回显
     * @param categoryId
     * @param map
     * @return
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "categoryId",required = false) Integer categoryId,
                              Map<String, Object> map) {
        if (categoryId != null) {
            //商品类目id不为空，显示单个商品类目
            ProductCategory productCategory = productCategoryService.findOne(categoryId);
            map.put("productCategory",productCategory);
        }
        return new ModelAndView("category/index", map);
    }

    /**
     * 更新/新增商品类目
     * @param categoryForm
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/save")
    public ModelAndView save(@Valid CategoryForm categoryForm, BindingResult bindingResult,
                             Map<String, Object> map) {
        if (bindingResult.hasErrors()) {
            map.put("msg", bindingResult.getFieldError().getDefaultMessage());
            map.put("url", "/sell/seller/category/index");
            return new ModelAndView("common/error", map);
        }
        ProductCategory category = new ProductCategory();

        //类目id是自增，不需要设置
        try {
            //修改
            if (categoryForm.getCategoryId() != null) {
                if (updateCheck(categoryForm, map)) {
                    //参数判断
                    return new ModelAndView("common/error", map);
                }
                BeanUtils.copyProperties(categoryForm, category);
                productCategoryService.save(category);
            }
            //新增
            if (saveCheck(categoryForm, map)) {
                //参数判断
                return new ModelAndView("common/error", map);
            }
            BeanUtils.copyProperties(categoryForm, category);
            productCategoryService.save(category);
        } catch (XFoodException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/category/index");
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.SUCCESS.getMessage());
        map.put("url", "/sell/seller/category/list");
        return new ModelAndView("common/success", map);
    }

    /**
     * 修改参数判断
     * @param categoryForm
     * @param map
     * @return
     */
    private boolean updateCheck(CategoryForm categoryForm, Map<String, Object> map) {
        List<ProductCategory> categoryList = productCategoryService.findAll();
        for (ProductCategory productCategory : categoryList) {
            if (categoryForm.getCategoryType() == productCategory.getCategoryType()) {
                //修改的type类型跟数据库的相同
                map.put("msg", ResultEnum.CATEGORY_TYPE_EXIST.getMessage());
                map.put("url", "/sell/seller/category/index?categoryId=" + categoryForm.getCategoryId() );
                return true;
            }
            if (categoryForm.getCategoryName().equals(productCategory.getCategoryName())) {
                //修改的类目名称跟数据库的相同
                map.put("msg", ResultEnum.CATEGORY_NAME_EXIST.getMessage());
                map.put("url", "/sell/seller/category/index?categoryId=" + categoryForm.getCategoryId() );
                return true;
            }
        }
        return false;
    }

    /**
     * 新增参数判断
     * @param categoryForm
     * @param map
     * @return
     */
    private boolean saveCheck(CategoryForm categoryForm, Map<String, Object> map) {
        List<ProductCategory> categoryList = productCategoryService.findAll();
        for (ProductCategory productCategory : categoryList) {
            if (categoryForm.getCategoryType() == productCategory.getCategoryType()) {
                //修改的type类型跟数据库的相同
                map.put("msg", ResultEnum.CATEGORY_TYPE_EXIST.getMessage());
                map.put("url", "/sell/seller/category/index");
                return true;
            }
            if (categoryForm.getCategoryName().equals(productCategory.getCategoryName())) {
                //修改的类目名称跟数据库的相同
                map.put("msg", ResultEnum.CATEGORY_NAME_EXIST.getMessage());
                map.put("url", "/sell/seller/category/index");
                return true;
            }
        }
        return false;
    }


}